<script setup lang="ts">
import { ref } from 'vue'
import { message, TreeProps } from 'ant-design-vue'
import IconFolder from '../../assets/svg/folder.svg'
import IconGitPush from '../../assets/svg/gitpush.svg'
import IconFolder1 from '../../assets/svg/folder1.svg'
import IconMd from '../../assets/svg/md.svg'
import IconAddFolder from '../../assets/svg/addFolder.svg'
import IconAddFile from '../../assets/svg/addFile.svg'
import IconFold from '../../assets/svg/fold.svg'
import IconOpenFold from '../../assets/svg/openFold.svg'
import IconSmile from '../../assets/svg/smile.svg'
import useConfigStore from '../../store/useConfigStore'
import { DownOutlined, FrownFilled, FrownOutlined } from '@ant-design/icons-vue'
import Tiptap from '../../components/tiptap/Tiptap.vue'
import Header from '../../components/Header.vue'

const { config } = useConfigStore()

const loading = ref(false)

const treeData = ref<TreeProps['treeData']>([])
const selectedKeys = ref([])
const leftWidth = ref(5)
const rightWidth = ref(20)

const viewIcon = ref(true)
const filePath = ref<string>('')

const aa = () => {
  if (leftWidth.value > 0) {
    leftWidth.value = 0
    rightWidth.value = 25
  } else {
    leftWidth.value = 5
    rightWidth.value = 20
  }
}

const controlExpandAll = (expand: boolean) => {
  console.log('expandAll', expand)
}
const controlViewIcon = () => {
  viewIcon.value = !viewIcon.value
}
const treeSelect = async (keys: string[]) => {
  console.log('treeSelect', keys)
  filePath.value = keys[0]
}
const initTreeData = async () => {
  loading.value = true
  console.log(config.project)
  await window.api.downloadViteTemplate({ ...config.project })
  const tree = await window.api.projectTree()
  console.log(tree)
  loading.value = false
  if (!tree) {
    message.warn('创建项目失败')
    return
  }
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  treeData.value = tree.filter((item: any) => {
    if (item.title === 'docs') {
      item.children = item.children.filter(
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        (child: any) => child.title != '.vitepress' && child.title != 'utils'
      )
      return true
    }
    return false
  })
}
// initTreeData()
</script>

<template>
  <Header />
  <div class="container" v-loading="loading">
    <div class="left">
      <div class="left-bottom-btn" @click="aa">
        <IconFolder />
      </div>
      <div class="left-bottom-btn" @click="aa">
        <IconGitPush />
      </div>
    </div>
    <div class="center">
      <div class="left-top" :style="leftWidth > 0 ? 'display: flex' : 'display: none'">
        <div>
          <IconAddFolder />
          <IconAddFile />
        </div>
        <div>
          <IconSmile @click="controlViewIcon" />
          <IconFold @click="controlExpandAll(false)" />
          <IconOpenFold @click="controlExpandAll(true)" />
        </div>
      </div>
      <div :style="leftWidth > 0 ? 'display: block' : 'display: none'" class="tree-container">
        <a-tree v-model:selectedKeys="selectedKeys" :tree-data="treeData" :show-icon="viewIcon" @select="treeSelect">
          <template #switcherIcon="{ switcherCls }">
            <down-outlined :class="switcherCls" />
          </template>
          <template #title="{ key: treeKey, title }">
            <a-dropdown :trigger="['contextmenu']">
              <span>{{ title }}</span>
              <template #overlay>
                <a-menu @click="({ key: menuKey }) => onContextMenuClick(treeKey, menuKey)">
                  <a-menu-item key="1">1st menu item</a-menu-item>
                  <a-menu-item key="2">2nd menu item</a-menu-item>
                  <a-menu-item key="3">3rd menu item</a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>
          </template>
          <template #icon="{ key, selected }">
            <template v-if="key === '0'">
              <IconFolder1 />
            </template>
            <template v-else-if="key === '0-0'">
              <IconFolder1 />
            </template>
            <template v-else-if="key === '0-1'">
              <IconFolder1 />
            </template>
            <template v-else-if="key === '0-0-0'">
              <IconMd />
            </template>
            <template v-else>
              <frown-filled v-if="selected" />
              <frown-outlined v-else />
            </template>
          </template>
        </a-tree>
      </div>
    </div>
    <div class="right">
      <tiptap :file-path="filePath"></tiptap>
      <div class="right-bottom">{{ filePath.replace('/', '').replace(/\//g, ' > ') }}</div>
    </div>
  </div>
</template>

<style scoped>
.container {
  display: flex;
  height: 100vh;
}

.left {
  background-color: #f3f2f2;
  flex: 0.6;
  padding: 30px 5px;
  position: relative;
}
.left::after {
  content: '';
  position: absolute;
  top: 30px;
  right: 0;
  width: 1px;
  height: calc(100% - 30px);
  background-color: rgba(200, 198, 198, 0.44);
}

.left-top {
  width: 100%;
  height: 30px;
  display: flex;
  align-items: center;
  padding: 40px 10px 10px 10px;
  justify-content: space-between;
}

.left-top > * > * {
  cursor: pointer;
  margin: 0 5px;
}

.left-top > * > *:hover {
  background-color: #c8c6c6;
  border-radius: 3px;
  border: 1px solid #c8c6c6;
}

.left-bottom-btn {
  cursor: pointer;
  display: flex;
  justify-content: center;
  background-color: #d8d6d6;
  width: 30px;
  height: 30px;
  margin: 6px 0;
  align-items: center;
  border-radius: 4px;
}

.tree-container :deep(.ant-tree .ant-tree-switcher) {
  width: 8px;
  margin: 0 8px 0 8px;
  line-height: 8px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.tree-container :deep(.ant-tree .ant-tree-node-content-wrapper .ant-tree-iconEle) {
  display: inline-flex;
  align-items: center;
}

.tree-container :deep(.ant-tree .ant-tree-node-content-wrapper.ant-tree-node-selected) {
  background-color: var(--primary-color);
}

.tree-container :deep(.ant-tree-list-holder) {
  background-color: #f3f2f2;
}

.tree-container :deep(.ant-tree .ant-tree-node-content-wrapper) {
  white-space: nowrap;
  overflow: hidden;
}

.center {
  flex: v-bind(leftWidth) !important;
  transition: flex 0.02s;
  background-color: #f3f2f2;
  overflow-y: auto;
  overflow-x: auto;
}

.right {
  background-color: rgba(243, 242, 242, 0.71);
  flex: v-bind(rightWidth) !important;
  transition: flex 0.02s;
  position: relative;
}
.right-bottom {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  position: absolute;
  background-color: #f7f8fa;
  width: 100%;
  bottom: 0;
  left: 0;
  padding: 5px 0 5px 5px;
  font-size: 0.9rem;
  color: #a9a9a9;
}
</style>
